package com.weimob.mcloud.wechatwork.customer.externalDependencyProxy.privateSphere;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.common.enums.CommonBizErrorEnum;
import com.weimob.mcloud.wechatwork.common.enums.MainBizDomainEnum;
import com.weimob.mcloud.wechatwork.common.enums.SubBizDomainEnum;
import com.weimob.mcloud.wechatwork.common.utils.BizErrorCodeGenerator;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.enums.ErrorCodeEnum;
import com.weimob.mcloud.wechatwork.statistics.exception.BizException;
import com.weimob.saas.mcloud.wechatwork.integration.common.request.BasicInfo;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import com.weimob.zipkin.ZipkinContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Objects;

import static com.weimob.mcloud.wechatwork.customer.service.user.enums.TagBizErrorEnum.AUTHORIZE_AUTH;

@Slf4j
@Component
public class PrivateSphereServiceProxy {

    @Reference
    private PrivateSphereService privateSphereService;

    /**
     * 企微助手产品id
     */
    @Value("${aewc.productId:203}")
    private Long aewcProductId;

    /**
     * 获取私域信息
     *
     * @param basicInfo basicInfo
     * @return privateSphereDTO
     */
    public PrivateSphere getPrivateSphereRequest(BasicInfo basicInfo) {
        Assert.notNull(basicInfo, "basicInfo must not be null");
        Assert.notNull(basicInfo.getBosId(), "bosId must not be null");
        Assert.notNull(basicInfo.getProductId(), "productId must not be null");
        //Assert.notNull(basicInfo.getProductInstanceId(), "productInstanceId must not be null");
        //入参组装
        Long bosId = basicInfo.getBosId();
        Long productId = basicInfo.getProductId();
        Long productInstanceId = basicInfo.getProductInstanceId();
        log.info("#PrivateSphereServiceProxy.convertPrivateSphere# request params:{}", JSON.toJSONString(basicInfo));
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setBosId(bosId);
        privateSphere.setProductId(productId);
        privateSphere.setProductInstanceId(productInstanceId);
        //查询私域id
        PrivateSphere privateSphereResponse = SoaUtil.unpack(privateSphereService.fetch(privateSphere));
        if (Objects.isNull(privateSphereResponse)) {
            String errorCode = BizErrorCodeGenerator.generate(MainBizDomainEnum.UNCLASSIFIED, SubBizDomainEnum.PRIVATE_SPHERE_RELATED, CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE);
            log.warn("#PrivateSphereServiceProxy.convertPrivateSphere# 私域获取失败, basicInfo:{}", JSON.toJSONString(basicInfo));
            throw new BizException(Long.parseLong(errorCode), CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE.getDesc());
        }
        String privateSphereId = privateSphereResponse.getPrivateSphereId();
        if (StringUtils.isEmpty(privateSphereId)) {
            throw new BizException("未获取到私域privateSphereId");
        }
        return privateSphereResponse;
    }


    public PrivateSphere getPrivateSphereRequest(String corpId) {
        if (StringUtils.isEmpty(corpId)) {
            log.error("PrivateSphereServiceProxy.getPrivateSphereRequest by corpId fail, cause corpId is null, globalTicket:{}", ZipkinContext.getContext().getGlobalTicket());
            throw new BizException(ErrorCodeEnum.GET_PRIVATE_SPHERE_FAILED_NULL_CORPID.getCode(), ErrorCodeEnum.GET_PRIVATE_SPHERE_FAILED_NULL_CORPID.getErrorDesc());
        }
        PrivateSphere privateSphere = new PrivateSphere();
        privateSphere.setCorpId(corpId);
        PrivateSphere privateSphereResult = SoaUtil.unpack(privateSphereService.fetch(privateSphere));
        if (Objects.isNull(privateSphereResult)) {
            String errorCode = BizErrorCodeGenerator.generate(MainBizDomainEnum.UNCLASSIFIED, SubBizDomainEnum.PRIVATE_SPHERE_RELATED, CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE);
            log.warn("#PrivateSphereServiceProxy.getPrivateSphereRequest# 私域获取失败");
            throw new BizException(Long.parseLong(errorCode), CommonBizErrorEnum.CANNOT_FIND_PRIVATE_SPHERE.getDesc());
        }
        String privateSphereId = privateSphereResult.getPrivateSphereId();
        if (StringUtils.isEmpty(privateSphereId)) {
            throw new BizException("未获取到私域privateSphereId");
        }
        return privateSphereResult;
    }

    public PrivateSphere queryPrivateSphere(Long bosId) {
        PrivateSphere request = new PrivateSphere();
        request.setBosId(bosId);
        request.setProductId(aewcProductId);
        SoaResponse<PrivateSphere, Void> fetchSoa = privateSphereService.fetch(request);
        if (0L != Long.parseLong(fetchSoa.getReturnCode())) {
            throw new BizException(Long.parseLong(AUTHORIZE_AUTH.getCode()), AUTHORIZE_AUTH.getDesc());
        }

        return fetchSoa.getResponseVo();
    }


}
